/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Student;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Linda de Haan
 */
public class ManageStudentController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Session session = SchoolContentManager.sessionFactory.getCurrentSession(); //get the session from the sessionFactory
        Transaction transaction = session.beginTransaction(); //start the transaction
        response.setContentType("text/html;charset=UTF-8");
        HttpSession sessie = request.getSession();
        Student student = null;

        if (sessie.getAttribute("manageStudentController").equals("editStudentSelect")) {
            student = (Student) session.get(Student.class, request.getParameter("student"));
            
            sessie.removeAttribute("msmsg");
            sessie.setAttribute("ucode", student.getuCode());
            sessie.setAttribute("email", student.getEmail());
            sessie.setAttribute("firstname", student.getFirstName());
            sessie.setAttribute("insertion", student.getInsertion());
            sessie.setAttribute("lastname", student.getLastName());
            sessie.setAttribute("bankaccount", student.getBankaccountnr());
            sessie.setAttribute("contentPage", "manageStudent.jsp");
            addHistori(sessie, "/manageStudentSelect.jsp");

        } else if (sessie.getAttribute("manageStudentController").equals("editStudent")) {
            student = (Student) session.get(Student.class, request.getParameter("ucode"));
            student.setEmail(request.getParameter("email"));
            student.setFirstName(request.getParameter("firstname"));
            student.setInsertion(request.getParameter("insertion"));
            student.setLastName(request.getParameter("lastname"));
            student.setBankaccountnr(request.getParameter("bankaccount"));
            
            session.saveOrUpdate(student);

            String sucodeSQL = "SELECT G.ucode FROM Gebruiker G WHERE G.ucode IN (SELECT S.ucode FROM Student S) order by G.ucode";
            String sfnameSQL = "SELECT G.firstname FROM Gebruiker G WHERE G.ucode IN (SELECT S.ucode FROM Student S) order by G.ucode";
            String sinsertSQL = "SELECT G.insertion FROM Gebruiker G WHERE G.ucode IN (SELECT S.ucode FROM Student S) order by G.ucode";
            String slnameSQL = "SELECT G.lastname FROM Gebruiker G WHERE G.ucode IN (SELECT S.ucode FROM Student S) order by G.ucode";

            ArrayList<String> sucode = (ArrayList) session.createSQLQuery(sucodeSQL).list();
            ArrayList<String> sfname = (ArrayList) session.createSQLQuery(sfnameSQL).list();
            ArrayList<String> sinsert = (ArrayList) session.createSQLQuery(sinsertSQL).list();
            ArrayList<String> slname = (ArrayList) session.createSQLQuery(slnameSQL).list();

            sessie.setAttribute("ucode", student.getuCode());
            sessie.setAttribute("email", student.getEmail());
            sessie.setAttribute("firstname", student.getFirstName());
            sessie.setAttribute("insertion", student.getInsertion());
            sessie.setAttribute("lastname", student.getLastName());
            sessie.setAttribute("bankaccount", student.getBankaccountnr());
            sessie.setAttribute("lenght", sucode.size());
            sessie.setAttribute("sucode", sucode);
            sessie.setAttribute("sfname", sfname);
            sessie.setAttribute("sinsert", sinsert);
            sessie.setAttribute("slname", slname);
            sessie.setAttribute("msmsg", "<b>Change Succesful!</b>");
            sessie.setAttribute("contentPage", "manageStudent.jsp");
            addHistori(sessie, "/manageStudentSelect.jsp");
        }

        transaction.commit();

        response.sendRedirect("index");


    }

    private void addHistori(HttpSession sessie, Object attribute) {
        List<Object> historie;
        if (sessie.getAttribute("lastpage") == null) {
            historie = new ArrayList();
        } else {
            historie = (ArrayList<Object>) sessie.getAttribute("lastpage");
        }
        System.out.println("terug naar" + attribute);
        historie.add(attribute);

        sessie.setAttribute("lastpage", historie);
    }

    private Object getLast(HttpSession sessie) {
        List<Object> historie = (List<Object>) sessie.getAttribute("lastpage");
        Object re = historie.get(historie.size() - 1);
        historie.remove(re);
        return re;
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
